function Get(url, data, cb) {
  wx.showNavigationBarLoading();
  wx.request({
    method: 'GET',
    url: url,
    data: data,
    success: (res) => {
      typeof cb == "function" && cb(res.data, "");
      wx.hideNavigationBarLoading();
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
      wx.hideNavigationBarLoading();
    }
  });
};

function Post(url, data, cb) {
  wx.showNavigationBarLoading();
  wx.request({
    method: 'POST',
    url: url,
    data: data,
    header: { "Content-Type": "application/x-www-form-urlencoded" },
    success: (res) => {
      typeof cb == "function" && cb(res.data, "");
      wx.hideNavigationBarLoading();
    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
      wx.hideNavigationBarLoading();
      console.log("http请求:" + config.HTTP_url);
      console.log(err)
    }
  });
};

function Upload(url, file, data, cb) {
  wx.uploadFile({
    url: url,
    filePath: file,
    name: "file",
    formData: data,
    success: (res) => {
      if (typeof (res.data) == "string") {
        typeof cb == "function" && cb(JSON.parse(res.data), "");
      } else {
        typeof cb == "function" && cb(res.data, "");
      }

    },
    fail: (err) => {
      typeof cb == "function" && cb(null, err.errMsg);
    }
  });
};
/*解密encryptedData */
function decodeEncrypte(url, iv, encryptedData, sessionKey, cb) {
  var data = {
    "sessionKey": sessionKey,
    'iv': iv,
    'encryptedData': encryptedData
  };
  Get(url, data, function (resForword) {
    typeof cb == "function" && cb(resForword)
  });
}

module.exports = {
  httpGet: Get,
  httpPost: Post,
  httpUpload: Upload,
  decodeEncrypte: decodeEncrypte
};